﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ListView.cs" company="DissingNellemann">
//   
// All rights reserved </copyright>
// <summary>
//   The list view.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Website.Models.ViewModels
{
    using System.Collections.Generic;

    using Website.Models.Styling;

    /// <summary>
    /// The i list view.
    /// </summary>
    /// <typeparam name="T">
    /// The type of object for which the listview is used.
    /// </typeparam>
    public class ListView<T>
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ListView{T}"/> class.
        /// </summary>
        /// <param name="items">
        /// The items.
        /// </param>
        /// <param name="skip">
        /// The skip.
        /// </param>
        /// <param name="take">
        /// The take.
        /// </param>
        /// <param name="totalRows">
        /// The total rows.
        /// </param>
        /// <param name="currentOrderBy">
        /// The current order by.
        /// </param>
        /// <param name="sortAscending">
        /// The sort ascending.
        /// </param>
        /// <param name="headerItems">
        /// The header items.
        /// </param>
        /// <param name="postAction">
        /// The post action.
        /// </param>
        public ListView(
            List<T> items,
            int skip,
            int take,
            int totalRows,
            string currentOrderBy,
            bool sortAscending,
            List<TableViewHeaderItem> headerItems,
            string postAction)
        {
            if (items != null)
            {
                this.List = items;
            }
            else
            {
                this.List = new List<T>();
            }

            this.NoPaging = false;
            this.Skip = skip;
            this.Take = take;
            this.TotalRows = totalRows;
            this.SortAscending = sortAscending;
            this.OrderBy = currentOrderBy;
            this.Header = TableViewHeader.GetTableHeader(postAction, take, currentOrderBy, headerItems, sortAscending);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ListView{T}"/> class.
        /// </summary>
        /// <param name="items">
        /// The items.
        /// </param>
        /// <param name="totalRows">
        /// The total rows.
        /// </param>
        /// <param name="headerItems">
        /// The header items.
        /// </param>
        public ListView(List<T> items, int totalRows, List<TableViewHeaderItem> headerItems)
        {
            if (items != null)
            {
                this.List = items;
            }
            else
            {
                this.List = new List<T>();
            }

            foreach (TableViewHeaderItem item in headerItems)
            {
                item.IsSortable = false;
            }

            this.NoPaging = false;
            this.TotalRows = totalRows;
            this.Header = TableViewHeader.GetTableHeader(string.Empty, 100000, string.Empty, headerItems, true);
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets the header.
        /// </summary>
        public TableViewHeader Header { get; private set; }

        /// <summary>
        ///     Gets the list.
        /// </summary>
        public List<T> List { get; private set; }

        /// <summary>
        ///     Gets or sets a value indicating whether no paging.
        /// </summary>
        public bool NoPaging { get; set; }

        /// <summary>
        ///     Gets the skip.
        /// </summary>
        public int Skip { get; private set; }

        /// <summary>
        ///     Gets the take.
        /// </summary>
        public int Take { get; private set; }

        /// <summary>
        ///     Gets the total rows.
        /// </summary>
        public int TotalRows { get; private set; }

        /// <summary>
        ///     Gets the indicator for sorting.
        /// </summary>
        public bool? SortAscending { get; private set; }

        /// <summary>
        ///     Gets the indicator for the current order by.
        /// </summary>
        public string OrderBy { get; private set; }

        #endregion
    }
}